#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

int g_var = 2 ;
char g_buf[] = "A string write to stdout.\n" ;

int main(int argc,char **argv)
{
	int	var = 80 ;
	pid_t	pid ;

	if( write(STDOUT_FILENO, g_buf, strlen(g_buf)) < 0 ) 
	{
		printf("Write string to stdout failed: %s\n", strerror(errno) ) ;
		return -1 ;
	}	

	printf("Before fork\n") ;

	if( (pid = fork() ) < 0  )
	{
		printf("Create fork() failed: %s\n", strerror(errno)) ;
		return -2 ;
	}
	
	else if( pid == 0 )
	{
		printf("Child process PID[%d] running...\n", getpid() ) ;
		g_var ++ ;
		var++ ;
	}
	else
	{
		printf("Parent process PID[%d], child PID[%d],And waiting...\n", getpid(), pid) ;
		sleep(1) ;
	}

	printf("PID = %ld, g_var = %d, var = %d\n", (long)getpid(), g_var, var) ;

	return 0 ;



}
